/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.ibrae.mchs.radiation.entities.controllers;

import ru.ibrae.mchs.radiation.entities.SubjectRf;
import ru.ibrae.mchs.radiation.entities.controllers.exceptions.IllegalOrphanException;
import ru.ibrae.mchs.radiation.entities.controllers.exceptions.NonexistentEntityException;
import ru.ibrae.mchs.radiation.entities.controllers.exceptions.PreexistingEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import ru.ibrae.mchs.radiation.entities.Result;
import java.util.HashSet;
import java.util.Set;
import ru.ibrae.mchs.radiation.entities.Rajon;
import ru.ibrae.mchs.radiation.entities.Selsovet;
import ru.ibrae.mchs.radiation.entities.RajonOld;
import ru.ibrae.mchs.radiation.entities.GSPos;
import ru.ibrae.mchs.radiation.entities.Geoobject;
import java.util.ArrayList;
import javax.persistence.TypedQuery;

/**
 *
 * @author murr
 */
public class SubjectRfJpaController {

    public SubjectRfJpaController() {
        emf = Persistence.createEntityManagerFactory("DesktopApplication4PU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(SubjectRf subjectRf) throws PreexistingEntityException, Exception {
        if (subjectRf.getResultSet() == null) {
            subjectRf.setResultSet(new HashSet<Result>());
        }
        if (subjectRf.getRajonSet() == null) {
            subjectRf.setRajonSet(new HashSet<Rajon>());
        }
        if (subjectRf.getSelsovetSet() == null) {
            subjectRf.setSelsovetSet(new HashSet<Selsovet>());
        }
        if (subjectRf.getRajonOldSet() == null) {
            subjectRf.setRajonOldSet(new HashSet<RajonOld>());
        }
        if (subjectRf.getGSPosSet() == null) {
            subjectRf.setGSPosSet(new HashSet<GSPos>());
        }
        if (subjectRf.getGeoobjectSet() == null) {
            subjectRf.setGeoobjectSet(new HashSet<Geoobject>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Set<Result> attachedResultSet = new HashSet<Result>();
            for (Result resultSetResultToAttach : subjectRf.getResultSet()) {
                resultSetResultToAttach = em.getReference(resultSetResultToAttach.getClass(), resultSetResultToAttach.getIdRezult());
                attachedResultSet.add(resultSetResultToAttach);
            }
            subjectRf.setResultSet(attachedResultSet);
            Set<Rajon> attachedRajonSet = new HashSet<Rajon>();
            for (Rajon rajonSetRajonToAttach : subjectRf.getRajonSet()) {
                rajonSetRajonToAttach = em.getReference(rajonSetRajonToAttach.getClass(), rajonSetRajonToAttach.getIdRajon());
                attachedRajonSet.add(rajonSetRajonToAttach);
            }
            subjectRf.setRajonSet(attachedRajonSet);
            Set<Selsovet> attachedSelsovetSet = new HashSet<Selsovet>();
            for (Selsovet selsovetSetSelsovetToAttach : subjectRf.getSelsovetSet()) {
                selsovetSetSelsovetToAttach = em.getReference(selsovetSetSelsovetToAttach.getClass(), selsovetSetSelsovetToAttach.getIdSelsov());
                attachedSelsovetSet.add(selsovetSetSelsovetToAttach);
            }
            subjectRf.setSelsovetSet(attachedSelsovetSet);
            Set<RajonOld> attachedRajonOldSet = new HashSet<RajonOld>();
            for (RajonOld rajonOldSetRajonOldToAttach : subjectRf.getRajonOldSet()) {
                rajonOldSetRajonOldToAttach = em.getReference(rajonOldSetRajonOldToAttach.getClass(), rajonOldSetRajonOldToAttach.getIdRajonO());
                attachedRajonOldSet.add(rajonOldSetRajonOldToAttach);
            }
            subjectRf.setRajonOldSet(attachedRajonOldSet);
            Set<GSPos> attachedGSPosSet = new HashSet<GSPos>();
            for (GSPos GSPosSetGSPosToAttach : subjectRf.getGSPosSet()) {
                GSPosSetGSPosToAttach = em.getReference(GSPosSetGSPosToAttach.getClass(), GSPosSetGSPosToAttach.getIdPoselen());
                attachedGSPosSet.add(GSPosSetGSPosToAttach);
            }
            subjectRf.setGSPosSet(attachedGSPosSet);
            Set<Geoobject> attachedGeoobjectSet = new HashSet<Geoobject>();
            for (Geoobject geoobjectSetGeoobjectToAttach : subjectRf.getGeoobjectSet()) {
                geoobjectSetGeoobjectToAttach = em.getReference(geoobjectSetGeoobjectToAttach.getClass(), geoobjectSetGeoobjectToAttach.getIdNp());
                attachedGeoobjectSet.add(geoobjectSetGeoobjectToAttach);
            }
            subjectRf.setGeoobjectSet(attachedGeoobjectSet);
            em.persist(subjectRf);
            for (Result resultSetResult : subjectRf.getResultSet()) {
                SubjectRf oldSubjectRfOfResultSetResult = resultSetResult.getSubjectRf();
                resultSetResult.setSubjectRf(subjectRf);
                resultSetResult = em.merge(resultSetResult);
                if (oldSubjectRfOfResultSetResult != null) {
                    oldSubjectRfOfResultSetResult.getResultSet().remove(resultSetResult);
                    oldSubjectRfOfResultSetResult = em.merge(oldSubjectRfOfResultSetResult);
                }
            }
            for (Rajon rajonSetRajon : subjectRf.getRajonSet()) {
                SubjectRf oldSubjectRfOfRajonSetRajon = rajonSetRajon.getSubjectRf();
                rajonSetRajon.setSubjectRf(subjectRf);
                rajonSetRajon = em.merge(rajonSetRajon);
                if (oldSubjectRfOfRajonSetRajon != null) {
                    oldSubjectRfOfRajonSetRajon.getRajonSet().remove(rajonSetRajon);
                    oldSubjectRfOfRajonSetRajon = em.merge(oldSubjectRfOfRajonSetRajon);
                }
            }
            for (Selsovet selsovetSetSelsovet : subjectRf.getSelsovetSet()) {
                SubjectRf oldSubjectRfOfSelsovetSetSelsovet = selsovetSetSelsovet.getSubjectRf();
                selsovetSetSelsovet.setSubjectRf(subjectRf);
                selsovetSetSelsovet = em.merge(selsovetSetSelsovet);
                if (oldSubjectRfOfSelsovetSetSelsovet != null) {
                    oldSubjectRfOfSelsovetSetSelsovet.getSelsovetSet().remove(selsovetSetSelsovet);
                    oldSubjectRfOfSelsovetSetSelsovet = em.merge(oldSubjectRfOfSelsovetSetSelsovet);
                }
            }
            for (RajonOld rajonOldSetRajonOld : subjectRf.getRajonOldSet()) {
                SubjectRf oldSubjectRfOfRajonOldSetRajonOld = rajonOldSetRajonOld.getSubjectRf();
                rajonOldSetRajonOld.setSubjectRf(subjectRf);
                rajonOldSetRajonOld = em.merge(rajonOldSetRajonOld);
                if (oldSubjectRfOfRajonOldSetRajonOld != null) {
                    oldSubjectRfOfRajonOldSetRajonOld.getRajonOldSet().remove(rajonOldSetRajonOld);
                    oldSubjectRfOfRajonOldSetRajonOld = em.merge(oldSubjectRfOfRajonOldSetRajonOld);
                }
            }
            for (GSPos GSPosSetGSPos : subjectRf.getGSPosSet()) {
                SubjectRf oldSubjectRfOfGSPosSetGSPos = GSPosSetGSPos.getSubjectRf();
                GSPosSetGSPos.setSubjectRf(subjectRf);
                GSPosSetGSPos = em.merge(GSPosSetGSPos);
                if (oldSubjectRfOfGSPosSetGSPos != null) {
                    oldSubjectRfOfGSPosSetGSPos.getGSPosSet().remove(GSPosSetGSPos);
                    oldSubjectRfOfGSPosSetGSPos = em.merge(oldSubjectRfOfGSPosSetGSPos);
                }
            }
            for (Geoobject geoobjectSetGeoobject : subjectRf.getGeoobjectSet()) {
                SubjectRf oldSubjectRfOfGeoobjectSetGeoobject = geoobjectSetGeoobject.getSubjectRf();
                geoobjectSetGeoobject.setSubjectRf(subjectRf);
                geoobjectSetGeoobject = em.merge(geoobjectSetGeoobject);
                if (oldSubjectRfOfGeoobjectSetGeoobject != null) {
                    oldSubjectRfOfGeoobjectSetGeoobject.getGeoobjectSet().remove(geoobjectSetGeoobject);
                    oldSubjectRfOfGeoobjectSetGeoobject = em.merge(oldSubjectRfOfGeoobjectSetGeoobject);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findSubjectRf(subjectRf.getId()) != null) {
                throw new PreexistingEntityException("SubjectRf " + subjectRf + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(SubjectRf subjectRf) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            SubjectRf persistentSubjectRf = em.find(SubjectRf.class, subjectRf.getId());
            Set<Result> resultSetOld = persistentSubjectRf.getResultSet();
            Set<Result> resultSetNew = subjectRf.getResultSet();
            Set<Rajon> rajonSetOld = persistentSubjectRf.getRajonSet();
            Set<Rajon> rajonSetNew = subjectRf.getRajonSet();
            Set<Selsovet> selsovetSetOld = persistentSubjectRf.getSelsovetSet();
            Set<Selsovet> selsovetSetNew = subjectRf.getSelsovetSet();
            Set<RajonOld> rajonOldSetOld = persistentSubjectRf.getRajonOldSet();
            Set<RajonOld> rajonOldSetNew = subjectRf.getRajonOldSet();
            Set<GSPos> GSPosSetOld = persistentSubjectRf.getGSPosSet();
            Set<GSPos> GSPosSetNew = subjectRf.getGSPosSet();
            Set<Geoobject> geoobjectSetOld = persistentSubjectRf.getGeoobjectSet();
            Set<Geoobject> geoobjectSetNew = subjectRf.getGeoobjectSet();
            List<String> illegalOrphanMessages = null;
            for (Rajon rajonSetOldRajon : rajonSetOld) {
                if (!rajonSetNew.contains(rajonSetOldRajon)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Rajon " + rajonSetOldRajon + " since its subjectRf field is not nullable.");
                }
            }
            for (Selsovet selsovetSetOldSelsovet : selsovetSetOld) {
                if (!selsovetSetNew.contains(selsovetSetOldSelsovet)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Selsovet " + selsovetSetOldSelsovet + " since its subjectRf field is not nullable.");
                }
            }
            for (RajonOld rajonOldSetOldRajonOld : rajonOldSetOld) {
                if (!rajonOldSetNew.contains(rajonOldSetOldRajonOld)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain RajonOld " + rajonOldSetOldRajonOld + " since its subjectRf field is not nullable.");
                }
            }
            for (GSPos GSPosSetOldGSPos : GSPosSetOld) {
                if (!GSPosSetNew.contains(GSPosSetOldGSPos)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain GSPos " + GSPosSetOldGSPos + " since its subjectRf field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Set<Result> attachedResultSetNew = new HashSet<Result>();
            for (Result resultSetNewResultToAttach : resultSetNew) {
                resultSetNewResultToAttach = em.getReference(resultSetNewResultToAttach.getClass(), resultSetNewResultToAttach.getIdRezult());
                attachedResultSetNew.add(resultSetNewResultToAttach);
            }
            resultSetNew = attachedResultSetNew;
            subjectRf.setResultSet(resultSetNew);
            Set<Rajon> attachedRajonSetNew = new HashSet<Rajon>();
            for (Rajon rajonSetNewRajonToAttach : rajonSetNew) {
                rajonSetNewRajonToAttach = em.getReference(rajonSetNewRajonToAttach.getClass(), rajonSetNewRajonToAttach.getIdRajon());
                attachedRajonSetNew.add(rajonSetNewRajonToAttach);
            }
            rajonSetNew = attachedRajonSetNew;
            subjectRf.setRajonSet(rajonSetNew);
            Set<Selsovet> attachedSelsovetSetNew = new HashSet<Selsovet>();
            for (Selsovet selsovetSetNewSelsovetToAttach : selsovetSetNew) {
                selsovetSetNewSelsovetToAttach = em.getReference(selsovetSetNewSelsovetToAttach.getClass(), selsovetSetNewSelsovetToAttach.getIdSelsov());
                attachedSelsovetSetNew.add(selsovetSetNewSelsovetToAttach);
            }
            selsovetSetNew = attachedSelsovetSetNew;
            subjectRf.setSelsovetSet(selsovetSetNew);
            Set<RajonOld> attachedRajonOldSetNew = new HashSet<RajonOld>();
            for (RajonOld rajonOldSetNewRajonOldToAttach : rajonOldSetNew) {
                rajonOldSetNewRajonOldToAttach = em.getReference(rajonOldSetNewRajonOldToAttach.getClass(), rajonOldSetNewRajonOldToAttach.getIdRajonO());
                attachedRajonOldSetNew.add(rajonOldSetNewRajonOldToAttach);
            }
            rajonOldSetNew = attachedRajonOldSetNew;
            subjectRf.setRajonOldSet(rajonOldSetNew);
            Set<GSPos> attachedGSPosSetNew = new HashSet<GSPos>();
            for (GSPos GSPosSetNewGSPosToAttach : GSPosSetNew) {
                GSPosSetNewGSPosToAttach = em.getReference(GSPosSetNewGSPosToAttach.getClass(), GSPosSetNewGSPosToAttach.getIdPoselen());
                attachedGSPosSetNew.add(GSPosSetNewGSPosToAttach);
            }
            GSPosSetNew = attachedGSPosSetNew;
            subjectRf.setGSPosSet(GSPosSetNew);
            Set<Geoobject> attachedGeoobjectSetNew = new HashSet<Geoobject>();
            for (Geoobject geoobjectSetNewGeoobjectToAttach : geoobjectSetNew) {
                geoobjectSetNewGeoobjectToAttach = em.getReference(geoobjectSetNewGeoobjectToAttach.getClass(), geoobjectSetNewGeoobjectToAttach.getIdNp());
                attachedGeoobjectSetNew.add(geoobjectSetNewGeoobjectToAttach);
            }
            geoobjectSetNew = attachedGeoobjectSetNew;
            subjectRf.setGeoobjectSet(geoobjectSetNew);
            subjectRf = em.merge(subjectRf);
            for (Result resultSetOldResult : resultSetOld) {
                if (!resultSetNew.contains(resultSetOldResult)) {
                    resultSetOldResult.setSubjectRf(null);
                    resultSetOldResult = em.merge(resultSetOldResult);
                }
            }
            for (Result resultSetNewResult : resultSetNew) {
                if (!resultSetOld.contains(resultSetNewResult)) {
                    SubjectRf oldSubjectRfOfResultSetNewResult = resultSetNewResult.getSubjectRf();
                    resultSetNewResult.setSubjectRf(subjectRf);
                    resultSetNewResult = em.merge(resultSetNewResult);
                    if (oldSubjectRfOfResultSetNewResult != null && !oldSubjectRfOfResultSetNewResult.equals(subjectRf)) {
                        oldSubjectRfOfResultSetNewResult.getResultSet().remove(resultSetNewResult);
                        oldSubjectRfOfResultSetNewResult = em.merge(oldSubjectRfOfResultSetNewResult);
                    }
                }
            }
            for (Rajon rajonSetNewRajon : rajonSetNew) {
                if (!rajonSetOld.contains(rajonSetNewRajon)) {
                    SubjectRf oldSubjectRfOfRajonSetNewRajon = rajonSetNewRajon.getSubjectRf();
                    rajonSetNewRajon.setSubjectRf(subjectRf);
                    rajonSetNewRajon = em.merge(rajonSetNewRajon);
                    if (oldSubjectRfOfRajonSetNewRajon != null && !oldSubjectRfOfRajonSetNewRajon.equals(subjectRf)) {
                        oldSubjectRfOfRajonSetNewRajon.getRajonSet().remove(rajonSetNewRajon);
                        oldSubjectRfOfRajonSetNewRajon = em.merge(oldSubjectRfOfRajonSetNewRajon);
                    }
                }
            }
            for (Selsovet selsovetSetNewSelsovet : selsovetSetNew) {
                if (!selsovetSetOld.contains(selsovetSetNewSelsovet)) {
                    SubjectRf oldSubjectRfOfSelsovetSetNewSelsovet = selsovetSetNewSelsovet.getSubjectRf();
                    selsovetSetNewSelsovet.setSubjectRf(subjectRf);
                    selsovetSetNewSelsovet = em.merge(selsovetSetNewSelsovet);
                    if (oldSubjectRfOfSelsovetSetNewSelsovet != null && !oldSubjectRfOfSelsovetSetNewSelsovet.equals(subjectRf)) {
                        oldSubjectRfOfSelsovetSetNewSelsovet.getSelsovetSet().remove(selsovetSetNewSelsovet);
                        oldSubjectRfOfSelsovetSetNewSelsovet = em.merge(oldSubjectRfOfSelsovetSetNewSelsovet);
                    }
                }
            }
            for (RajonOld rajonOldSetNewRajonOld : rajonOldSetNew) {
                if (!rajonOldSetOld.contains(rajonOldSetNewRajonOld)) {
                    SubjectRf oldSubjectRfOfRajonOldSetNewRajonOld = rajonOldSetNewRajonOld.getSubjectRf();
                    rajonOldSetNewRajonOld.setSubjectRf(subjectRf);
                    rajonOldSetNewRajonOld = em.merge(rajonOldSetNewRajonOld);
                    if (oldSubjectRfOfRajonOldSetNewRajonOld != null && !oldSubjectRfOfRajonOldSetNewRajonOld.equals(subjectRf)) {
                        oldSubjectRfOfRajonOldSetNewRajonOld.getRajonOldSet().remove(rajonOldSetNewRajonOld);
                        oldSubjectRfOfRajonOldSetNewRajonOld = em.merge(oldSubjectRfOfRajonOldSetNewRajonOld);
                    }
                }
            }
            for (GSPos GSPosSetNewGSPos : GSPosSetNew) {
                if (!GSPosSetOld.contains(GSPosSetNewGSPos)) {
                    SubjectRf oldSubjectRfOfGSPosSetNewGSPos = GSPosSetNewGSPos.getSubjectRf();
                    GSPosSetNewGSPos.setSubjectRf(subjectRf);
                    GSPosSetNewGSPos = em.merge(GSPosSetNewGSPos);
                    if (oldSubjectRfOfGSPosSetNewGSPos != null && !oldSubjectRfOfGSPosSetNewGSPos.equals(subjectRf)) {
                        oldSubjectRfOfGSPosSetNewGSPos.getGSPosSet().remove(GSPosSetNewGSPos);
                        oldSubjectRfOfGSPosSetNewGSPos = em.merge(oldSubjectRfOfGSPosSetNewGSPos);
                    }
                }
            }
            for (Geoobject geoobjectSetOldGeoobject : geoobjectSetOld) {
                if (!geoobjectSetNew.contains(geoobjectSetOldGeoobject)) {
                    geoobjectSetOldGeoobject.setSubjectRf(null);
                    geoobjectSetOldGeoobject = em.merge(geoobjectSetOldGeoobject);
                }
            }
            for (Geoobject geoobjectSetNewGeoobject : geoobjectSetNew) {
                if (!geoobjectSetOld.contains(geoobjectSetNewGeoobject)) {
                    SubjectRf oldSubjectRfOfGeoobjectSetNewGeoobject = geoobjectSetNewGeoobject.getSubjectRf();
                    geoobjectSetNewGeoobject.setSubjectRf(subjectRf);
                    geoobjectSetNewGeoobject = em.merge(geoobjectSetNewGeoobject);
                    if (oldSubjectRfOfGeoobjectSetNewGeoobject != null && !oldSubjectRfOfGeoobjectSetNewGeoobject.equals(subjectRf)) {
                        oldSubjectRfOfGeoobjectSetNewGeoobject.getGeoobjectSet().remove(geoobjectSetNewGeoobject);
                        oldSubjectRfOfGeoobjectSetNewGeoobject = em.merge(oldSubjectRfOfGeoobjectSetNewGeoobject);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = subjectRf.getId();
                if (findSubjectRf(id) == null) {
                    throw new NonexistentEntityException("The subjectRf with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            SubjectRf subjectRf;
            try {
                subjectRf = em.getReference(SubjectRf.class, id);
                subjectRf.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The subjectRf with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Set<Rajon> rajonSetOrphanCheck = subjectRf.getRajonSet();
            for (Rajon rajonSetOrphanCheckRajon : rajonSetOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This SubjectRf (" + subjectRf + ") cannot be destroyed since the Rajon " + rajonSetOrphanCheckRajon + " in its rajonSet field has a non-nullable subjectRf field.");
            }
            Set<Selsovet> selsovetSetOrphanCheck = subjectRf.getSelsovetSet();
            for (Selsovet selsovetSetOrphanCheckSelsovet : selsovetSetOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This SubjectRf (" + subjectRf + ") cannot be destroyed since the Selsovet " + selsovetSetOrphanCheckSelsovet + " in its selsovetSet field has a non-nullable subjectRf field.");
            }
            Set<RajonOld> rajonOldSetOrphanCheck = subjectRf.getRajonOldSet();
            for (RajonOld rajonOldSetOrphanCheckRajonOld : rajonOldSetOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This SubjectRf (" + subjectRf + ") cannot be destroyed since the RajonOld " + rajonOldSetOrphanCheckRajonOld + " in its rajonOldSet field has a non-nullable subjectRf field.");
            }
            Set<GSPos> GSPosSetOrphanCheck = subjectRf.getGSPosSet();
            for (GSPos GSPosSetOrphanCheckGSPos : GSPosSetOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This SubjectRf (" + subjectRf + ") cannot be destroyed since the GSPos " + GSPosSetOrphanCheckGSPos + " in its GSPosSet field has a non-nullable subjectRf field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Set<Result> resultSet = subjectRf.getResultSet();
            for (Result resultSetResult : resultSet) {
                resultSetResult.setSubjectRf(null);
                resultSetResult = em.merge(resultSetResult);
            }
            Set<Geoobject> geoobjectSet = subjectRf.getGeoobjectSet();
            for (Geoobject geoobjectSetGeoobject : geoobjectSet) {
                geoobjectSetGeoobject.setSubjectRf(null);
                geoobjectSetGeoobject = em.merge(geoobjectSetGeoobject);
            }
            em.remove(subjectRf);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<SubjectRf> findSubjectRfEntities() {
        return findSubjectRfEntities(true, -1, -1);
    }

    public List<SubjectRf> findSubjectRfEntities(int maxResults, int firstResult) {
        return findSubjectRfEntities(false, maxResults, firstResult);
    }

    private List<SubjectRf> findSubjectRfEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(SubjectRf.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public SubjectRf findSubjectRf(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(SubjectRf.class, id);
        } finally {
            em.close();
        }
    }

    public int getSubjectRfCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<SubjectRf> rt = cq.from(SubjectRf.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

    public SubjectRf findSubjectRfByName(String name) {
        EntityManager em = getEntityManager();
        String text = "SELECT ID FROM subject_rf WHERE NAME1 = '" + name + "'";
        Query q = em.createNativeQuery(text);
        //q.setParameter(1, name);
        return q.getResultList().isEmpty() ? null : findSubjectRf((Integer) q.getSingleResult());
    }
}
